package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.BlackList;
import com.wanlian.entity.WordBook;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.BlackListServices;
import com.wanlian.service.WordBookServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/wordbook")
public class WordBookController {


    @Autowired
    public WordBookServices wordBookServices;


    /**
     * 添加字典
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data,HttpSession session) {
        WordBook wordBook = JsonStringUtil.readValue(data, WordBook.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(user==null){
            return new JsonResult().Serailze(false,ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = wordBookServices.doAdd(wordBook,userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 禁用或启用字典
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String edit(@RequestBody String data,HttpSession session) {
        WordBook wordBook = JsonStringUtil.readValue(data, WordBook.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(user==null){
            return new JsonResult().Serailze(false,ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = wordBookServices.doEnable(wordBook,userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询字典
     * @param data
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = wordBookServices.queryByCondition(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按ID查询字典
     * @param data
     * @return
             */
    @RequestMapping(value = "/querybyId", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listById(@RequestBody String data) {
        WordBook wordBook = JsonStringUtil.readValue(data, WordBook.class);
        ResultModel resultModel = wordBookServices.queryById(wordBook.getId());
        return new JsonResult().Serailze(resultModel);

    }

    /**
     * 修改
     * @param data
     * @return
     */
    @RequestMapping(value = "/upd", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String editwordbook(@RequestBody String data,HttpSession session) {
        WordBook wordBook = JsonStringUtil.readValue(data, WordBook.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(user==null){
            return new JsonResult().Serailze(false,ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = wordBookServices.doEdit(wordBook,userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/getchannel", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String querychannel(HttpSession session) {
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(user==null){
            return new JsonResult().Serailze(false,ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = user.getLoginUser().getId();
        ResultModel resultModel = wordBookServices.queryChannel();
        return new JsonResult().Serailze(resultModel);
    }

}
